<?php

namespace app\api\controller\v1\store\base;

use app\admin\model\DataStoreAlbum;
use app\admin\model\DataStorePhoto;
use app\api\controller\Api;

class Photo extends Api
{
    public function get(){
        $rule = [
            'album_id'    => 'require',
        ];
        $message  =   [
            'album_id.require' => '相册不为空',
        ];
        $this->validate($this->post, $rule,$message);
        $list = $this->getModel()->where(['album_id'=>$this->post['album_id']])->findOrEmpty();
        if($list->isEmpty())$this->error('图片不存在');
        $this->success('获取成功',$list);
    }

    public function set(){

        $rule = [
            'album_id'    => 'require',
            'image'    => 'require',
        ];
        $message  =   [
            'album_id.require' => '相册不为空',
            'image.require' => '图片不为空',
        ];
        $this->validate($this->post, $rule,$message);
        $album = DataStoreAlbum::where(['id'=>$this->post['album_id']])->findOrEmpty();
        if($album->isEmpty())$this->error('相册不存在');
        $image = explode(',',$this->post['image']);
        $data = [];
        foreach ($image as $v){
            $data[] = [
                'album_id' =>     $this->post['album_id'],
                'image'    =>      $v
            ];
        }
        $list = new DataStorePhoto();
        $list->saveAll($data);
        if($list){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }


    /**
     * 批量删除相册
     * @return void
     */
    public function remove(){
        $rule = [
            'image_id'    => 'require',
        ];
        $message  =   [
            'image_id.require' => '图片不为空',
        ];
        $this->validate($this->post, $rule,$message);
        $image = explode(',',$this->post['image_id']);
        $success =0;
        foreach ($image as $value){
            $delete = $this->getModel()->findOrEmpty($value);
            if(!$delete->isEmpty()){
                $delete_save= $delete->delete();
                if($delete_save)$success++;
            }

        }
        $this->success('删除成功'.$success.'个');

    }



    private function getModel(){
        return new DataStorePhoto();
    }

}